package com.extweb4j.web.controller;

import com.extweb4j.core.anno.Log;
import com.extweb4j.core.controller.ExtController;
import com.extweb4j.core.enums.RowStatus;
import com.extweb4j.core.kit.ExtKit;
import com.extweb4j.core.model.ExtTask;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.List;

/**
 * 任务控制器
 */
public class TaskController extends ExtController {

    /**
     * 列表
     */

    public void list() {
        int page = getParaToInt("page");
        int limit = getParaToInt("limit");
        String keywords = getPara("keywords");
        String where = " 1=1 ";
        String sort = "id";
        Page<ExtTask> pageData = ExtTask.dao.pageDataBy(page, limit, keywords, sort, "DESC", where, null);
        renderJson(pageData);
    }

    /**
     * 新增
     */
    @Log("创建任务")

    @Before(Tx.class)
    public void add() {
        //新增用户
        ExtTask task = getExtModel(ExtTask.class);
        task.set("id", ExtKit.UUID());
        task.save();
        success();
    }

    /**
     * 编辑
     */
    @Log("编辑任务")

    @Before(Tx.class)
    public void edit() {
        ExtTask task = getExtModel(ExtTask.class);
        task.update();
        success();
    }

    /**
     * 逻辑删除
     */
    @Log("删除任务")

    @Before(Tx.class)
    public void delete() {
        ExtTask task = ExtTask.dao.findById(getPara("id"));
        if (task == null) {
            throw new RuntimeException("要删除的任务不存在");
        }
        task.delete();
        success();
    }

    /**
     * 获取所有可用任务
     */
    public void json() {

        List<ExtTask> list = ExtTask.dao.findAll();
        renderJson(list);
    }

}
